var express = require('express');
var app = express();
var bodyParser = require('body-parser');//用于req.body获取值的
// 处理静态资源
app.use(express.static('public'))
// 处理参数
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
//  ------------------
//  这个文件时在学习vue的前端交互时用于测试的
// -------------------

app.get('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "PUT,GET,POST,DELETE,OPTIONS");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-hEADERS", "Content-Type");
  next();
});

app.get('/', function (req, res) {
  res.send('hello world');
});

app.get('/a1', function (req, res) {
  setTimeout(() => {
    res.send('hello world1111');
  }, 1000);
});

app.get('/a2', function (req, res) {
  setTimeout(() => {
    res.send('hello world2222');
  }, 1500);
});

app.get('/a3', function (req, res) {
  setTimeout(() => {
    res.send('hello world3333');
  }, 2000);
});

app.get('/fetch', function (req, res) {
  res.send('传统的URL传参测参数' + req.query.id);
});

app.get('/fetch/:id', function (req, res) {
  res.send('REST形式的URL传参测参数1' + req.params.id);
});

app.post('/fetch/books', function (req, res) {
  res.header("Access-Control-Allow-Origin", "*");
  console.log('~~~~', req.body.uname, "````", req.body.pwd)
  var uname = req.body.uname
  var pwd = req.body.pwd
  res.send("REST形式的URL传参测参数2:" + uname + " ---- " + pwd);
});

app.get('/fetch/test/json', function (req, res) {
  res.json({
    "name": "wwj"
  })
});


app.get('/axios', function (req, res) {
  // res.header("Access-Control-Allow-Origin", "*");
  res.send('REST形式的URL传参测参数1--axios')
});
app.get('/axios/parm', function (req, res) {
  var id = req.query.id
  res.send('REST形式的URL传参测参数22--axios==' + id)
});
app.get('/axios/parm2/:id', function (req, res) {
  var id = req.params.id
  res.send('REST形式的URL传参测参数333--axios==' + id)
});
app.post('/axios2', function (req, res) {
  res.header("Access-Control-Allow-Origin", "*");
  var uname = req.body.uname
  var pwd = req.body.pwd
  res.send('REST形式的URL传参测参数333--axios==' + uname + "---" + pwd)
});




app.listen(3002, function () {
  console.log('访问```：127.0.0.1:3002');
});